package com.mq.rabbitmqtrac.controller;

import com.mq.rabbitmqtrac.client.BlogClient;
import com.mq.rabbitmqtrac.sender.RabbitSender;
import com.mq.rabbitmqtrac.service.MessageProviderService;
import com.mq.rabbitmqtrac.util.MQConstants;
import com.mq.rabbitmqtrac.util.RabbitMetaMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Slf4j
public class MessageProviderController {

    /**
     * test if can get value from config center
     **/
    @Value("${spring.rabbitmq.host}")
    private String rabbitmqAddress;

    @Autowired
    private RabbitSender rabbitSender;

    @Autowired
    private BlogClient blogClient;

    @Autowired
    private MessageProviderService messageProviderService;

    @RequestMapping(value = "testconfig", method = RequestMethod.GET)
    public String testconfig() throws Exception {
        return rabbitmqAddress;
    }

    @RequestMapping(value = "testmq", method = RequestMethod.GET)
    @Transactional
    public String testMessage() throws Exception {
        /** 生成一个发送对象 */
        RabbitMetaMessage rabbitMetaMessage = new RabbitMetaMessage();
        /**设置交换机 */
        rabbitMetaMessage.setExchange(MQConstants.BUSINESS_EXCHANGE);
        /**指定routing key */
        rabbitMetaMessage.setRoutingKey(MQConstants.BUSINESS_KEY);
        /** 设置需要传递的消息体,可以是任意对象 */
        rabbitMetaMessage.setPayload("the message you want to send");

        //do some biz

        /** 发送消息 */
        rabbitSender.send(rabbitMetaMessage);

        return "sucess";
    }

    @RequestMapping(value = "testDB", method = RequestMethod.GET)
    public String testDB() throws Exception {
        log.info(messageProviderService.findUserById(1L).toString());

        log.info(blogClient.getBlog(1L).toString());
        return "sucess";
    }

}
